package com.ffcs.cn.action.scene;

import com.ffcs.cn.action.common.AbstractPanelForm;
import com.ffcs.cn.action.common.FormModel;
import com.supermap.desktop.controls.ui.controls.DialogResult;
import com.supermap.desktop.controls.ui.controls.datasetChoose.DatasetChooseMode;
import com.supermap.desktop.controls.ui.controls.datasetChoose.SmDatasetChooser;
import com.supermap.desktop.core.Application;
import com.supermap.desktop.core.Interface.IBaseItem;
import com.supermap.desktop.core.Interface.IFormScene;
import com.supermap.desktop.core.enums.WindowType;
import com.supermap.desktop.core.implement.CtrlAction;
import com.supermap.desktop.core.utilties.FormUtilities;
import com.supermap.desktop.core.utilties.TableUtilities;
import com.ffcs.cn.DevelopProperties;

import com.supermap.realspace.Scene;

import java.awt.event.ActionListener;
import java.util.List;

/**
 * 打开场景
 *
  */
public class CtrlActionOpenScene extends CtrlAction {
	public CtrlActionOpenScene(IBaseItem caller) {
		super(caller);
	}

	@Override
	public void run() {
		new DialogSelectScene().showDialog();
	}

	private static class DialogSelectScene extends AbstractPanelForm {

		public DialogSelectScene() {
			setTitle(DevelopProperties.getString("String_Title_OpenScene"));
		}

		@Override
		public FormModel getTableModel() {
			return new FormModel() {
				private final String[] columnNames = {DevelopProperties.getString("String_SceneName")};

				@Override
				public Object getValueAt(int row, int column) {
					Object bean = tableModel.get(row);
					if (column == 0) {
						return bean;
					}
					return null;
				}

				@Override
				public int getColumnCount() {
					return columnNames.length;
				}

				@Override
				public String getColumnName(int column) {
					return columnNames[column];
				}

				@Override
				public Class<?> getColumnClass(int columnIndex) {
					return Scene.class;
				}

				/**
				 * 如果是具有相同名称的场景，则视为同一个场景
				 *
				 * @param scene 场景对象
				 * @return 该场景是否存在
				 */
				@Override
				public boolean contains(Object scene) {
					String name = ((Scene) scene).getName();
					List<Object> data = getData();
					for (Object val : data) {
						if (((Scene) val).getName().equals(name)) {
							return true;
						}
					}
					return false;
				}
			};
		}

		/**
		 * 点击添加按钮，选择将要打开的场景
		 *
		 * @return 点击添加按钮后触发事件的监听器
		 */
		@Override
		public ActionListener getAddActionListener() {
			return e -> {
				SmDatasetChooser smDatasetChooser = new SmDatasetChooser(DatasetChooseMode.SCENE);
				if (smDatasetChooser.showDialog() == DialogResult.OK) {
					List<Object> selectedObjects = smDatasetChooser.getSelectedObjects();
					for (Object obj : selectedObjects) {
						if (obj instanceof Scene && !tableModel.contains(obj)) {
							int rowCount = tableModel.getRowCount();
							tableModel.add(obj);
							TableUtilities.setTableSelectedRows(getItemTable(), rowCount);
						}
					}
				}
				smDatasetChooser.dispose();
			};
		}


		/**
		 * 点击确定按钮，将选择的场景依次打开
		 *
		 * @return 点击确认按钮后触发事件的监听器
		 */
		@Override
		public ActionListener getOkActionListener() {
			return e -> {
				try {
					// 获取选择的场景
					Object[] data = tableModel.getData().toArray();
					for (Object datum : data) {
						if (datum instanceof Scene) {
							// 打开一个空白场景
							IFormScene formScene = (IFormScene) FormUtilities.fireNewWindowEvent(WindowType.SCENE);
							// 根据名称打开指定场景
							formScene.openScene(((Scene) datum).getName());
						}
					}
				} catch (Exception exception) {
					Application.getActiveApplication().getOutput().output(exception);
				} finally {
					this.dispose();
				}
			};
		}
	}
}
